package com.ming.tflitesingleblur;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import com.ming.tflitesingleblur.segment.Classifier;
import com.ming.tflitesingleblur.utils.ImageSaveUtils;
import com.ming.tflitesingleblur.utils.NV21ToBitmap;
import com.ming.tflitesingleblur.utils.PermissionUtils;
import com.ming.tflitesingleblur.utils.ToastUtils;

import java.io.IOException;

public class Gl20SurfaceViewActivity extends BaseActivity {

    private GLSurfaceView mGLSurfaceView;
    private CameraGL20Render mRender;

    private Classifier mClassifier;
    private NV21ToBitmap mNv21ToBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gl_surface_view);
        requestNotAddFingerLayout();

        //申请权限
        PermissionUtils.requestPerssions(this, PERMISSIONS_REQUEST, PERMISSION_CAMERA, PERMISSION_STORAGE,PERMISSION_STORAGE_READ);
        if(PermissionUtils.hasPermissons(this,PERMISSION_CAMERA, PERMISSION_STORAGE, PERMISSION_STORAGE_READ)){
            initView();
        }

    }

    private void initView() {
        Log.i(TAG, "initView: ===============");
        //开启相机
        mCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
        mCamera = Camera.open(mCameraId);
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.set("orientation", "portrait");
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        parameters.setPreviewSize(mPreviewWidth, mPreviewHeight);
        mCamera.setDisplayOrientation(90);
        mCamera.setParameters(parameters);

        mCamera.setPreviewCallback(previewCallback);

        mGLSurfaceView = (GLSurfaceView) findViewById(R.id.glsurfaceview);

        //把GLSurfaceView的尺寸改成与预览尺寸比例一致,防止预览变形
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mGLSurfaceView.getLayoutParams();
        layoutParams.width = ImageSaveUtils.getDisplayWidth(this);
        layoutParams.height = (int) (ImageSaveUtils.getDisplayWidth(this) * (float)mPreviewWidth / (float)mPreviewHeight);
//        layoutParams.height = 500;
        mGLSurfaceView.setLayoutParams(layoutParams);
        mGLSurfaceView.requestLayout();

        //创建渲染器
        mRender = new CameraGL20Render(mContext, mGLSurfaceView, mCamera);

        //给GLSurfaceView设置渲染器,它要跟使用的东西同步，比如这里设置了1，那么在Render
        // 中就使用GL10，如果这里设置了2，就在Renderer中使用GL20，要对应起来
        mGLSurfaceView.setEGLContextClientVersion(2);
        mGLSurfaceView.setRenderer(mRender);
        //渲染模式
        /**
         * 1.自动模式
         setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
         2."脏"模式
         setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
         当需要重绘时，调用
         GLSurfaceView.requestRender()
         一般情况下使用脏模式，这样可以有效降低cpu负载。
         */
        mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
//        mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        //如果上面设置的是自动模式，那只用启动一次即可，否则需要手动调用requestRender
        mGLSurfaceView.requestRender();

        handlerThread = new HandlerThread("inference");
        handlerThread.start();
        inferenceHandler = new Handler(handlerThread.getLooper());
        try {
            mClassifier = Classifier.create(Gl20SurfaceViewActivity.this,model,device, numThreads);
        } catch (IOException e) {
            e.printStackTrace();
        }

        mNv21ToBitmap = new NV21ToBitmap(mContext);

        findViewById(R.id.btn_click).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mRender.startRecord();
                ToastUtils.showToast("开始录制");

                //延时停止录制
//                mHandler.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        mRender.stopRecord();
//                        ToastUtils.showToast("录制完成");
//                    }
//                }, 5000);

            }
        });
    }

    int mCameraId;
    Camera mCamera;

    private Classifier.Model model = Classifier.Model.HUMAN_DANCE;
    private Classifier.Device device = Classifier.Device.CPU;
    private int numThreads = -1;


    private static final String PERMISSION_CAMERA = Manifest.permission.CAMERA;
    private static final String PERMISSION_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;
    private static final String PERMISSION_STORAGE_READ = Manifest.permission.READ_EXTERNAL_STORAGE;

    private static final int PERMISSIONS_REQUEST = 1;

    HandlerThread handlerThread;
    Handler inferenceHandler;

    private byte[] mNv21;
    private volatile boolean isSegment = false;
    int mPreviewWidth = 640;
    int mPreviewHeight = 480;
    private Camera.PreviewCallback previewCallback = new Camera.PreviewCallback() {
        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {
            if (mNv21 == null || data.length != mNv21.length) {
                mNv21 = new byte[data.length];
            }

            if(!isSegment){
                isSegment = true;
                synchronized (mNv21) {
                    System.arraycopy(data, 0, mNv21, 0, data.length);
                    inferenceHandler.post(classifyRunnable);
                }
            }
        }
    };

    String baseImagePath = "/sdcard/workspace/tfblur/";
    String inputImage = baseImagePath +"input/";
    String maskImage = baseImagePath +"mask/";
    Runnable classifyRunnable = new Runnable() {
        @Override
        public void run() {
            Bitmap bitmapImg = mNv21ToBitmap.nv21ToBitmap(mNv21,mPreviewWidth, mPreviewHeight);
            long timeStamp = System.currentTimeMillis();

            //把输入图旋转保存
//            Matrix matrix = new Matrix();
//            matrix.postRotate(90);
//            Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapImg,
//                    bitmapImg.getWidth(),
//                    bitmapImg.getHeight(),
//                    true);
//            Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap,
//                    0, 0,
//                    scaledBitmap.getWidth(),
//                    scaledBitmap.getHeight(),
//                    matrix, true);
//
//            ImageSaveUtils.saveBitmap(rotatedBitmap,inputImage,timeStamp+".png");

            //先把nv21转化成图片
           float[] mask = mClassifier.segment(bitmapImg, 90);//传0表示图不需要旋转,右旋90
            //把mask转化成标准内容(0,1,2)
//            float[] mask = ImageSaveUtils.generateSegmentMask(predictData,
//                    new int[]{mClassifier.getOutputWidth(), mClassifier.getImageHeight(), 3});


            mGLSurfaceView.queueEvent(new Runnable() {
                @Override
                public void run() {
                    mRender.setMaskData(mClassifier, mask);
                }
            });
//            Log.i(TAG, "run: "+ mask.length +" " + Arrays.toString(mask));
//            ImageSaveUtils.saveImage(maskImage,
//                    timeStamp+".png",
//                    mask,
//                    new int[]{mClassifier.getOutputWidth(),mClassifier.getImageHeight(),1},
//                    true,
//                    new float[]{0, 255f});
           //保存图片看看是什么东西
            isSegment = false;
        }
    };


    @Override
    public void onRequestPermissionsResult(
            final int requestCode, final String[] permissions, final int[] grantResults) {
        super.onRequestPermissionsResult(requestCode,permissions,grantResults);
        Log.i(TAG, "onRequestPermissionsResult: ==========" + requestCode);
        if (requestCode == PERMISSIONS_REQUEST) {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED
                    && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                //权限申请成功，重新调用一下onCreate
                onCreate(null);
            } else {
                toast("please give me permissions");
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.release();
        }
        Log.i(TAG, "onPause=================");
    }

    @Override
    protected void onDestroy() {
        Log.i(TAG, "onDestory=================");
        super.onDestroy();
    }
}
